package com.bw.util;

import javax.imageio.metadata.IIOInvalidTreeException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    public static int getAge(Calendar taeget){
        int age=0;
        Calendar instance = Calendar.getInstance();
        if(instance.before(taeget)){
            throw new  IllegalArgumentException("参数不正确");
        }
        int day_year = instance.get(Calendar.YEAR);
        int day_MONTH = instance.get(Calendar.MONTH);
        int day_day = instance.get(Calendar.DAY_OF_MONTH);
        int target_year = taeget.get(Calendar.YEAR);
        int target_Month = taeget.get(Calendar.MONTH);
        int target_day = instance.get(Calendar.DAY_OF_MONTH);
        age=day_day - target_day;
        if(day_MONTH<=target_Month){
            if(day_MONTH == target_Month){
                age--;
            }else{
                age--;
            }
        }
        return age;
    }


    public static Integer getAge(String birthday){
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        Date parse=null;
        try {
            parse = sd.parse(birthday);
        } catch (ParseException e) {
            System.out.println("参数格式有误");
            return null;
        }
        Calendar instance = Calendar.getInstance();
        instance.setTime(parse);
        int age = DateUtil.getAge(instance);
        return age;
    }
    public static int getpastDays(Calendar target){
        int day=0;
        long time = Calendar.getInstance().getTime().getTime();

        long time1 = target.getTime().getTime();
        if(time-time1<0){
            throw  new IllegalArgumentException("参数不合法异常");
        }else {
            day=(int) ((time-time1) / 1000 / 60 / 60 / 24);
        }
        return day;
    }

    // 判断给定的日期是否为今天
    public static boolean isInThisDay(Character target){
        boolean resuit = false;
        if(target==null)
            return false;
        Calendar toDay = Calendar.getInstance();
        return true;

    }
}
